home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Misc / a2 / Source / screen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-21  |  5.9 KB  |  282 lines

  1. /*
  2.  *  a2, an Apple II emulator in C
  3.  *  (c) Copyright 1990 by Rich Skrenta
  4.  *
  5.  *  Command line interface written by Tom Markson
  6.  *
  7.  *  Distribution agreement:
  8.  *
  9.  *    You may freely copy or redistribute this software, so long
  10.  *    as there is no profit made from its use, sale, trade or
  11.  *    reproduction.  You may not change this copyright notice,
  12.  *    and it must be included prominently in any copy made.
  13.  *
  14.  *  Send emulator related mail to:  skrenta@blekko.commodore.com
  15.  *                    skrenta@blekko.uucp
  16.  */
  17.  
  18.  
  19.  
  20. #import    <stdio.h>
  21. #import <libc.h>
  22. #import    "a2.h"
  23.  
  24. extern char screen_map[];        /* at the bottom of this file */
  25. extern int text1[];
  26.  
  27.  
  28. int line1[0x400];
  29. int col1[0x400];
  30.  
  31.  
  32. void info(char *s)
  33. {
  34.  
  35.     MoveCursor(0, 57);
  36.     printf("%-20s", s);
  37.  
  38.     last_line = -1;
  39.     last_col = -1;
  40.     fflush(stdout);
  41. }
  42.  
  43. #define appleout(n)    if (n >= 0x80) putchar(screen_map[n]); else { StartInverse(); putchar(screen_map[n]); EndInverse(); }
  44.  
  45.  
  46. void set_text1f(unsigned short addr, unsigned char n)
  47. {
  48. int line, col;
  49. unsigned char c;
  50.  
  51.     if (mem[addr] == n)        /* no change, don't do anything */
  52.         return;
  53.  
  54.     mem[addr] = n;
  55.     line = line1[addr - 0x400];
  56.     col = col1[addr - 0x400];
  57.  
  58.     if (line == -1)
  59.         return;
  60.  
  61. /*
  62.  *  Stuff to try to make updating more efficient
  63.  *  Is it really worth it?
  64.  */
  65.  
  66.     if (line == last_line) {
  67.         if (col == 0)
  68.             putchar('\r');
  69.         else switch (col - last_col) {
  70.         case 4:
  71.             c = mem[addr - 3]; appleout(c);
  72.             c = mem[addr - 2]; appleout(c);
  73.             c = mem[addr - 1]; appleout(c);
  74.             break;
  75.         case 3:
  76.             c = mem[addr - 2]; appleout(c);
  77.             c = mem[addr - 1]; appleout(c);
  78.             break;
  79.         case 2:
  80.             c = mem[addr - 1]; appleout(c);
  81.             break;
  82.         case 1:            /* already there */
  83.             break;
  84.         case 0:
  85.             putchar('\b');
  86.             break;
  87.         case -1:
  88.             putchar('\b'); putchar('\b');
  89.             break;
  90.         case -2:
  91.             putchar('\b'); putchar('\b'); putchar('\b');
  92.             break;
  93.         case -3:
  94.             putchar('\b'); putchar('\b'); putchar('\b');
  95.             putchar('\b');
  96.             break;
  97.         default:
  98.             MoveCursor(line, col);
  99.         }
  100.     } else
  101.         MoveCursor(line, col);
  102.  
  103.     appleout(n);
  104.     fflush(stdout);
  105.  
  106.     last_line = line;
  107.     last_col = col;
  108. }
  109.  
  110. void screen_setup(void)
  111. {
  112.   int line, col;
  113.   int addr, base;
  114.   int i;
  115.  
  116.   for (i = 0; i < 0x400; i++) {
  117.     line1[i] = -1;
  118.     col1[i] = -1;
  119.   }
  120.  
  121.   mem_set[0x04] = set_text1f;
  122.   mem_set[0x05] = set_text1f;
  123.   mem_set[0x06] = set_text1f;
  124.   mem_set[0x07] = set_text1f;
  125.  
  126.   for (line = 0; line < 24; line++) {
  127.     base = text1[line];
  128.     for (col = 0; col < 40; col++) {
  129.       addr = base + col;
  130.       mem[addr] = 0xA0;
  131.       line1[addr - 0x400] = line;
  132.       col1[addr - 0x400] = col;
  133.     }
  134.   }
  135. }
  136.  
  137.  
  138. int last_line = -1;
  139. int last_col = -1;
  140.  
  141.  
  142.  
  143. void set_text1(unsigned short addr, unsigned char n)
  144. {
  145. int line, col;
  146. unsigned char c;
  147.  
  148.     if (mem[addr] == n)        /* no change, don't do anything */
  149.         return;
  150.  
  151.     mem[addr] = n;
  152.     line = line1[addr - 0x400];
  153.     col = col1[addr - 0x400];
  154.  
  155.     if (line == -1)
  156.         return;
  157.  
  158. /*
  159.  *  Stuff to try to make updating more efficient
  160.  *  Is it really worth it?
  161.  */
  162.  
  163.     if (line == last_line) {
  164.         if (col == 0)
  165.             putchar('\r');
  166.         else switch (col - last_col) {
  167.         case 4:
  168.             c = mem[addr - 3]; appleout(c);
  169.             c = mem[addr - 2]; appleout(c);
  170.             c = mem[addr - 1]; appleout(c);
  171.             break;
  172.         case 3:
  173.             c = mem[addr - 2]; appleout(c);
  174.             c = mem[addr - 1]; appleout(c);
  175.             break;
  176.         case 2:
  177.             c = mem[addr - 1]; appleout(c);
  178.             break;
  179.         case 1:            /* already there */
  180.             break;
  181.         case 0:
  182.             putchar('\b');
  183.             break;
  184.         case -1:
  185.             putchar('\b'); putchar('\b');
  186.             break;
  187.         case -2:
  188.             putchar('\b'); putchar('\b'); putchar('\b');
  189.             break;
  190.         case -3:
  191.             putchar('\b'); putchar('\b'); putchar('\b');
  192.             putchar('\b');
  193.             break;
  194.         default:
  195.             MoveCursor(line, col);
  196.         }
  197.     } else
  198.         MoveCursor(line, col);
  199.  
  200.     appleout(n);
  201.  
  202.     last_line = line;
  203.     last_col = col;
  204. }
  205.  
  206.  
  207. void redraw_screen(void)
  208. {
  209. int i, j;
  210. unsigned short base;
  211. unsigned char c;
  212.  
  213.     ClearScreen();
  214.     for (i = 0; i < 24; i++) {
  215.         base = text1[i];
  216.         for (j = 0; j < 40; j++) {
  217.             c = mem[base + j];
  218.             appleout(c);
  219.         }
  220.         if (i < 23)
  221.             putchar('\n');
  222.     }
  223.  
  224.     last_line = -1;
  225.     last_col = -1;
  226.  
  227.     fflush(stdout);
  228. }
  229.  
  230.  
  231. /*
  232.  *  Screen display mapping table
  233.  */
  234.  
  235. char screen_map[] = {
  236.  
  237.     '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',        /* $00    */
  238.     'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',        /* $08    */
  239.     'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',        /* $10    */
  240.     'X', 'Y', 'Z', '[', '\\',']', '^', '_',        /* $18    */
  241.     ' ', '!', '"', '#', '$', '%', '&', '\'',    /* $20    */
  242.     '(', ')', '*', '+', ',', '-', '.', '/',        /* $28    */
  243.     '0', '1', '2', '3', '4', '5', '6', '7',        /* $30    */
  244.     '8', '9', ':', ';', '<', '=', '>', '?',        /* $38    */
  245.  
  246.     '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',        /* $40    */
  247.     'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',        /* $48    */
  248.     'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',        /* $50    */
  249.     'X', 'Y', 'Z', '[', '\\',']', '^', '_',        /* $58    */
  250.     ' ', '!', '"', '#', '$', '%', '&', '\'',    /* $60    */
  251.     '(', ')', '*', '+', ',', '-', '.', '/',        /* $68    */
  252.     '0', '1', '2', '3', '4', '5', '6', '7',        /* $70    */
  253.     '8', '9', ':', ';', '<', '=', '>', '?',        /* $78    */
  254.  
  255.     '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g',        /* $80    */
  256.     'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',        /* $88    */
  257.     'p', 'q', 'r', 's', 't', 'u', 'v', 'w',        /* $90    */
  258.     'x', 'y', 'z', '[', '\\',']', '^', '_',        /* $98    */
  259.     ' ', '!', '"', '#', '$', '%', '&', '\'',    /* $A0    */
  260.     '(', ')', '*', '+', ',', '-', '.', '/',        /* $A8    */
  261.     '0', '1', '2', '3', '4', '5', '6', '7',        /* $B0    */
  262.     '8', '9', ':', ';', '<', '=', '>', '?',        /* $B8    */
  263.  
  264.     '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',        /* $C0    */
  265.     'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',        /* $C8    */
  266.     'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',        /* $D0    */
  267.     'X', 'Y', 'Z', '[', '\\',']', '^', '_',        /* $D8    */
  268.     ' ', '!', '"', '#', '$', '%', '&', '\'',    /* $E0    */
  269.     '(', ')', '*', '+', ',', '-', '.', '/',        /* $E8    */
  270.     '0', '1', '2', '3', '4', '5', '6', '7',        /* $F0    */
  271.     '8', '9', ':', ';', '<', '=', '>', '?',        /* $F8    */
  272.  
  273. };
  274.  
  275. int text1[] = {
  276.     0x400, 0x480, 0x500, 0x580, 0x600, 0x680, 0x700, 0x780,
  277.     0x428, 0x4A8, 0x528, 0x5A8, 0x628, 0x6A8, 0x728, 0x7A8,
  278.     0x450, 0x4D0, 0x550, 0x5D0, 0x650, 0x6D0, 0x750, 0x7D0,
  279.     0x478, 0x4F8, 0x578, 0x5F8, 0x678, 0x6F8, 0x778, 0x7F8
  280. };
  281.  
  282.